package com.ruoyi.comment.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.mapper.ArticleMapper;
import com.ruoyi.article.service.IArticleService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Service;
import com.ruoyi.comment.mapper.CommentMapper;
import com.ruoyi.comment.domain.Comment;
import com.ruoyi.comment.service.ICommentService;

import javax.annotation.Resource;

/**
 * 文章点评管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class CommentServiceImpl implements ICommentService 
{
    @Resource
    private CommentMapper commentMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询文章点评管理
     * 
     * @param id 文章点评管理主键
     * @return 文章点评管理
     */
    @Override
    public Comment selectCommentById(Integer id)
    {
        return commentMapper.selectCommentById(id);
    }

    /**
     * 查询文章点评管理列表
     * 
     * @param comment 文章点评管理
     * @return 文章点评管理
     */
        @Resource
        private IArticleService articleService;
    @Override
    public List<Comment> selectCommentList(Comment comment, String nickName, String articleTitle)
    {
        System.out.println("comment"+comment);
        Long userId = SecurityUtils.getLoginUser().getUserId();
        Article article1 = new Article();
        article1.setUserId(userId);
        List<Article> articles = articleService.selectArticleList(article1,null,null);
        List<Integer> articleIds = new ArrayList<>();
        if(!articles.isEmpty()){
            for (Article article : articles) {
                articleIds.add(article.getId());
            }
        }

        SysUser sysUser = new SysUser();
        Article article = new Article();
        if (nickName != null){
            sysUser.setNickName(nickName);
        }else {
            sysUser.setNickName(null);
        }

        if (articleTitle != null){
            article.setTitle(articleTitle);
        }else {
            article.setTitle(null);
        }

        String date = comment.getDate();
        if (date != null && !"".equals(date)){
            String[] split = date.split("to");
            Map<String,Object> map = new HashMap<>();
            map.put("beginDate",split[0]);
            map.put("endDate",split[1]);
            comment.setParams(map);
        }
        comment.setSysUser(sysUser);
        comment.setArticle(article);

        List<Comment> comments = new ArrayList<>();
        for (Integer articleId : articleIds) {
            comment.setArticleId(articleId);
            List<Comment> comments1 = commentMapper.selectCommentList(comment);
            comments.addAll(comments1);
        }
        return comments;
    }

    /**
     * 新增文章点评管理
     * 
     * @param comment 文章点评管理
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
//        redisCache.getCacheObject()
        if (comment.getDate() == null || "".equals(comment.getDate())) {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            comment.setDate(sdf.format(date));
        }
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改文章点评管理
     * 
     * @param comment 文章点评管理
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除文章点评管理
     * 
     * @param ids 需要删除的文章点评管理主键
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Integer[] ids)
    {
        return commentMapper.deleteCommentByIds(ids);
    }

    /**
     * 删除文章点评管理信息
     * 
     * @param id 文章点评管理主键
     * @return 结果
     */
    @Override
    public int deleteCommentById(Integer id)
    {
        return commentMapper.deleteCommentById(id);
    }

    @Override
    public Integer getCommentCount(Integer articleId){
        return commentMapper.getCommentCount(articleId);
    }
}
